#!/usr/bin/env python

from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor

class LoggingProtocol(LineReceiver):
    MAX_LENGTH = 500
    
    
    def lineReceived(self, line):
        self.factory.fp.write(line+'\n')
    
class LogfileFactory(Factory):
    protocol = LoggingProtocol
    
    def __init__(self, fileName):
        self.file = fileName
        
    def startFactory(self):
        self.fp = open(self.file, 'a') 
        
    def stopFactory(self):
        self.fp.close()
        
endpoint = TCP4ServerEndpoint(reactor, 8007)
endpoint.listen(LogfileFactory("FileToWrite.txt"))
reactor.run()